/*
* $Id: HousingDialog.cpp 165 2010-10-14 18:26:07Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "HousingDialog.h"
#include "Components/Journal/HousingManager.h"

CHousingDialog::CHousingDialog()
: CDialog(IDD_NEW_HOUSE, dynamic_cast<CWnd*>(IVisualManager::GetVisualManager()->
		  GetVisualComponent(ILanguageManager::GetLanguageManager()->GetString(JOURNAL_CAPTION_ID))->GetComponentFrame())),
	m_nFrames(0), m_nIdHousing(0), m_nIdFamily(0), m_nTypeHousing(0), m_bUpdate(false){
		GetTypes();
}
CHousingDialog::CHousingDialog(int nId)
: CDialog(IDD_NEW_HOUSE, dynamic_cast<CWnd*>(IVisualManager::GetVisualManager()->
		  GetVisualComponent(ILanguageManager::GetLanguageManager()->GetString(JOURNAL_CAPTION_ID))->GetComponentFrame())),
	m_nIdHousing(nId), m_bUpdate(true){
		CHousingManager::housing hive = IJournalComponent::GetHousingManager()->GetHousing(nId);
		m_strName = hive.m_strName;
		m_nTypeHousing = hive.m_typeHousing;
		m_nFrames = hive.m_typeframes;
		m_nIdFamily = hive.m_nIdFamily;
		GetTypes();
}
CHousingDialog::~CHousingDialog(){}

void CHousingDialog::GetTypes(){
	std::map<int, std::tstring> aTypes = IJournalComponent::GetHousingManager()->GetType(Housing::HOUSING);
	for(std::map<int, std::tstring>::iterator it = aTypes.begin(); it != aTypes.end(); it++)
		m_aTypesHousing.insert(std::make_pair(it->first, it->second));
	aTypes = IJournalComponent::GetHousingManager()->GetType(Housing::FRAME);
	for(std::map<int, std::tstring>::iterator it = aTypes.begin(); it != aTypes.end(); it++)
			m_aTypesFrame.insert(std::make_pair(it->first, it->second));
}

BOOL CHousingDialog::OnInitDialog(){
	BOOL bRes = __super::OnInitDialog();
	SetWindowText(ILanguageManager::GetLanguageManager()->GetString(ADD_HOUSING_ID).c_str());
	SetDlgItemText(IDC_NAME_HOUSE, ILanguageManager::GetLanguageManager()->GetString(HOUSING_NAME_ID).c_str());
	SetDlgItemText(IDC_TYPE_HOUSE, ILanguageManager::GetLanguageManager()->GetString(HOUSING_TYPE_ID).c_str());
	SetDlgItemText(IDC_TYPE_FRAME, ILanguageManager::GetLanguageManager()->GetString(HOUSING_FRAME_ID).c_str());
	if(m_bUpdate)
		SetDlgItemText(IDOK, ILanguageManager::GetLanguageManager()->GetString(CHANGE_ID).c_str());
	else
		SetDlgItemText(IDOK, ILanguageManager::GetLanguageManager()->GetString(ADD_ID).c_str());
	SetDlgItemText(IDCANCEL, ILanguageManager::GetLanguageManager()->GetString(CANCEL_ID).c_str());

	SetDlgItemText(IDC_NAMEH_EDIT, m_strName.c_str());

	for(std::map<int, std::tstring>::iterator it = m_aTypesHousing.begin(); it != m_aTypesHousing.end(); it++){
		int cur = m_typeHousing.AddString(it->second.c_str());
		m_typeHousing.SetItemData(cur, it->first);
		if(it->first == m_nTypeHousing)
			m_typeHousing.SetCurSel(cur);
	}

	for(std::map<int, std::tstring>::iterator it = m_aTypesFrame.begin(); it != m_aTypesFrame.end(); it++){
		int cur = m_typeFrame.AddString(it->second.c_str());
		m_typeFrame.SetItemData(cur, it->first);
		if(it->first == m_nTypeHousing)
			m_typeFrame.SetCurSel(cur);
	}
	return bRes;
}

void CHousingDialog::DoDataExchange(CDataExchange* pDX){
	DDX_Control(pDX, IDC_TYPEH_EDIT, m_typeHousing);
	DDX_Control(pDX, IDC_TYPEF_EDIT, m_typeFrame);
	__super::DoDataExchange(pDX);
}

void CHousingDialog::OnOK(){
	CString strValue;
	GetDlgItemText(IDC_NAMEH_EDIT, strValue);
	m_strName = strValue.GetString();
	m_nTypeHousing = (int)m_typeHousing.GetItemData(m_typeHousing.GetCurSel());
	m_nFrames = (int)m_typeFrame.GetItemData(m_typeFrame.GetCurSel());
	CHousingManager* pManager = IJournalComponent::GetHousingManager();
	CHousingManager::housing hive;
	hive.m_strName = m_strName;
	hive.m_typeHousing = m_nTypeHousing;
	hive.m_typeframes = m_nFrames;
	hive.m_nIdFamily = m_nIdFamily;
	if(m_bUpdate){
		hive.m_bDelete = false;
		pManager->UpdateHousing(m_nIdHousing, hive);
	}else
		m_nIdHousing = pManager->AddHousing(hive);
	EndDialog(IDOK);
}

int CHousingDialog::GetIdHousing(){
	return m_nIdHousing;
}

std::tstring CHousingDialog::GetNameHousing(){
	return m_strName;
}

int CHousingDialog::GetTypeHousing(){
	return m_nTypeHousing;
}

int CHousingDialog::GetTypeFrames(){
	return m_nFrames;
}
